import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_controller.dart';
import 'package:flutterproject/page/mine/children/assets/data/assets_detail_bean.dart';
import 'package:flutterproject/page/mine/data/my_assets_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:get/get.dart';

class MyAssetsController extends BaseController {
  int currentIndex = Get.arguments['index'];

  final TextEditingController textEditingController =
      TextEditingController(text: "0.00");

  AssetsDetailBean? assetsDetailBean = AssetsDetailBean();
  MyAssetsBean myAssetsBean = Get.arguments['id'];
  HttpClient client = Get.find<HttpClient>();

  @override
  void onReady() {
    super.onReady();
    getAssetsDetail();
  }

  Future getAssetsDetail() async {
    try {
      DialogUtil.showLoading();
      assetsDetailBean = await client.get<AssetsDetailBean>(
        AddressManager.assetsDetail,
        queryParameters: {
          "id": myAssetsBean.list?[currentIndex].id,
        },
      );
      update();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  final List<Tab> myTabs = <Tab>[
    Tab(text: '酝积分'),
    Tab(text: '绿色积分'),
    Tab(text: '权益积分'),
    Tab(text: '众商通'),
    Tab(text: '创投积分'),
  ];

  void changeIndex(int index) {
    currentIndex = index;
    getAssetsDetail();
  }

  /// 兑换
  Future exchange(String pwd, String money, int type) async {
    try {
      DialogUtil.showLoading();
      assetsDetailBean = await client.post(
        AddressManager.exchange,
        data: {
          "value": money,
          "walletId": myAssetsBean.list?[currentIndex].id,
          "password": pwd,
          "targetType": type
        },
      );
      ToastUtil.showSuccess("兑换成功!");
      Get.back();
      getAssetsDetail();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  /// 互转
  Future transform({String? mobile, String? pwd, String? money}) async {
    try {
      DialogUtil.showLoading();
      assetsDetailBean = await client.post(
        AddressManager.transform,
        data: {
          "value": money,
          "mobile": mobile,
          "password": pwd,
          "walletId": myAssetsBean.list?[currentIndex].id,
        },
      );
      ToastUtil.showSuccess("互转成功!");
      Get.back();
      getAssetsDetail();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  /// 提取
  Future withdraw() async {
    DialogUtil.showPwdDialog((value) async {
      try {
        DialogUtil.showLoading();
        assetsDetailBean = await client.post(
          AddressManager.withdraw,
          data: {
            "value": textEditingController.text,
            "walletId": myAssetsBean.list?[currentIndex].id,
            "password": value,
          },
        );
        ToastUtil.showSuccess("提取成功!");
        Get.back();
        getAssetsDetail();
      } on HttpException catch (e) {
        ToastUtil.showFailed(e.message);
      } finally {
        DialogUtil.dismiss();
      }
    });
  }
}
